package com.app.controller;

import com.app.dao.UserDao;
import com.app.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
	private static Logger log = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserDao userDao;

	@RequestMapping(value = "/addRandomUser",method = RequestMethod.GET)
	public String addRandomUser(ModelMap model) {
		model.addAttribute("message", "Hello world!");
		User  user = new User();
		int random = (int)(Math.random()*100);
		user.setName("李"+String.valueOf(random));
		user.setAge(random);
		user.setSex("男");
		int result = this.userDao.insertUser(user);
		model.addAttribute("result", result);
		return "hello";
	}

	@RequestMapping(value = "/batchUpdate")
	public String batchUpdate(ModelMap model, HttpServletRequest request) {
		List<Map<String, Object>> list = new ArrayList<>();
		for (int i = 19; i < 22; i++) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", i);
			map.put("name", "li_" + i);
			map.put("age", 14);
			map.put("sex", "w");
			list.add(map);
		}
		try {
			int successNum = userDao.batchUpdate(list);
			return String.valueOf(successNum);
		}catch (Exception e){
			log.error(e.getMessage());
		}

		return "success";
	}
	@RequestMapping(value = "/batchInsert")
	public String batchInsert(ModelMap model, HttpServletRequest request) {
		List<Map<String, Object>> list = new ArrayList<>();
		for (int i = 22; i < 25; i++) {
			Map<String, Object> map = new HashMap<>();
			map.put("name", "li_" + i);
			map.put("age", 15);
			map.put("sex", "w");
			list.add(map);
		}
		try {
			int successNum = userDao.batchInsert(list);
			return String.valueOf(successNum);
		}catch (Exception e){
			log.error(e.getMessage());
			return "error";
		}

	}


	@RequestMapping(value = "/testSimpleJdbc")
	public String testSimpleJdbc() {
		try {
			this.userDao.testSimpleUpdate();
			return "success";
		} catch (Exception e) {
			log.info(e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/testJdbcTemplate")
	public String testJdbcTemplate() {
		try {
			this.userDao.testJdbcTemplateInsertNull();
			return "success";
		} catch (Exception e) {
			log.info(e.getMessage());
			return e.getMessage();
		}
	}

	public static Logger getLog() {
		return log;
	}

	public static void setLog(Logger log) {
		UserController.log = log;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}

// i fixed a bug on issue-110